package jdk5.concurrent.tiger;

import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import static java.lang.System.out;

public class ReverseStringCallable implements Callable<String> {
         private String in;

        public ReverseStringCallable(String in){
               this.in = in;
        }

        public String call(){
                 try{
                       Thread.sleep(3000);
                  }catch(Exception e){}

                 StringBuffer sb  = new StringBuffer(in);
                 return sb.reverse().toString();
        }
        
        public static void main(String[] args){
              ExecutorService es = Executors.newSingleThreadExecutor();
              Callable<String> c = new ReverseStringCallable("Hello");
              Future<String> f = es.submit(c);
               int sum = 0;
              for(int i = 1; i <= 100; i++) sum += i; 
              out.printf("sum = %d%n", sum);
              try{
                    out.printf("wait for the result of the Future%n");
                    out.printf("reverse('hello') = %s%n", f.get()); 
 	 es.shutdown();
              }catch(Exception e){}                           
        }
}

